<?php
/**
 * Created by PhpStorm.
 * User: XZR
 * Date: 2018/8/6
 * Time: 15:14
 */

namespace Admin\Model;



use Think\Model;

class RecordModel extends Model
{
    protected $tablePrefix = "";
    protected $tableName = 'tb_md_medical_record';


    private static $instance;

    public static function singleton()
    {
        self::$instance === null and self::$instance = new self();
        return self::$instance;
    }

    //获取记录总数
    public function getCount($map){

        $res =  $this->alias('r')
            ->join('__TB_MD_USER__ u on r.user_id = u.id','Left')
            ->join('__TB_MD_MEDICAL_RECORD_DETAIL__ rd on r.id = rd.record_id','Left')
            ->where($map)
            ->field('r.*,u.name,u.hospital')
            ->order("r.create_time desc")
            ->group('r.id')
            ->select();
        return count($res);

    }

    public function getList($map,$limit){

        $res =  $this->alias('r')
            ->join('__TB_MD_USER__ u on r.user_id = u.id','Left')
            ->join('__TB_MD_MEDICAL_RECORD_DETAIL__ rd on r.id = rd.record_id','Left')
            ->where($map)
            ->field('r.*,rd.*,r.id rid,r.is_show,u.name doctor_name,u.hospital,r.create_time as createdate')
            ->limit($limit)
            ->order("r.create_time desc")
            ->group('r.id')
            ->select()?:[];
        //echo $this->getLastSql();
        return $res;

    }

    public function getOne($id){
        return $this->where(['id'=>$id])->find();
    }

    public function delete($id){
        $data['is_show']   =   0;
        $id    = is_array($id) ? implode(',',$id) : $id;
        $where['id']=array('IN',$id);
        return $this->where($where)->save($data);
    }
    public function show($id){
        $data['visible']   =   'Y';
        $id    = is_array($id) ? implode(',',$id) : $id;
        $where['id']=array('IN',$id);
        return $this->where($where)->save($data);
    }
    public function hide($id){
        $data['visible']   =   'N';
        $id    = is_array($id) ? implode(',',$id) : $id;
        $where['id']=array('IN',$id);
        return $this->where($where)->save($data);
    }
    public function getRecordDetailList($record_id){
        return $this->table('__TB_MD_MEDICAL_RECORD_DETAIL__')
            ->where(['record_id'=>$record_id])
            ->field('*')
            ->order('create_time DESC')
            ->select();

    }

    public function getRecordDetail($id){
        return $this->table('__TB_MD_MEDICAL_RECORD_DETAIL__')
            ->where(['id'=>$id])
            ->find();

    }

    //增加点评数
    public function incCommentCount($id){
        $where['id']=$id;
        return $this->where($where)->setInc('comment_count');
    }

    //减少点评数
    public function descCommentCount($id){
        $where['id']=$id;
        return $this->where($where)->setDec('comment_count');
    }

    //减少点评数
    public function descNewCommentCount($id){
        $where['id']=$id;
        return $this->where($where)->setDec('new_comment_count');
    }
}